package com.itheima.stargraph.service.impl;

import com.itheima.stargraph.service.WsNotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

/**
 * WebSocket消息推送业务实现类
 *
 * @Author mr.wu
 * @Date 2025-6-12 11:44
 */
@Service
public class WsNotifyServiceImpl implements WsNotifyService {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    private static final String URL = "/topic/messages";


    /**
     * 一对一推送，推送消息给指定客户端（指定用户）
     *
     * @param clientId
     * @param msg
     */
    @Override
    public void sendMsgToUser(String clientId, String msg) {
        //调用这个方法后，会自动的在URL前拼接/user/clientId的值
        simpMessagingTemplate.convertAndSendToUser(clientId,URL,msg);
    }

    /**
     * 广播消息推送，推送给全部客户端（全部用户）
     *
     * @param msg
     */
    @Override
    public void sendMsgToAll(String msg) {
        simpMessagingTemplate.convertAndSend(URL,msg);
    }
}
